//-----------------------------------------------------------------------
// <copyright file="a_CountryDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_CountryDAOBase
    {
        #region Common methods
        public virtual a_Country Createa_CountryFromReader(IDataReader reader)
        {
            a_Country item = new a_Country();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CountryID"))) item.CountryID = (int)reader["CountryID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CountryNameVN"))) item.CountryNameVN = (string)reader["CountryNameVN"];
                if (!reader.IsDBNull(reader.GetOrdinal("CountryNameEN"))) item.CountryNameEN = (string)reader["CountryNameEN"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Country methods
            
        public virtual void Createa_Country(a_Country a_Country)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Countries_ins");
                
                database.AddInParameter(dbCommand, "@CountryNameVN", DbType.String, a_Country.CountryNameVN);
                database.AddInParameter(dbCommand, "@CountryNameEN", DbType.AnsiString, a_Country.CountryNameEN);
                database.AddOutParameter(dbCommand, "@CountryID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Country.CountryID = (int)database.GetParameterValue(dbCommand, "@CountryID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Country methods
        
        public virtual void Updatea_Country(a_Country a_Country)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Countries_upd");            
                
                database.AddInParameter(dbCommand, "@CountryID", DbType.Int32, a_Country.CountryID);
                database.AddInParameter(dbCommand, "@CountryNameVN", DbType.String, a_Country.CountryNameVN);
                database.AddInParameter(dbCommand, "@CountryNameEN", DbType.AnsiString, a_Country.CountryNameEN);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Country methods
        public virtual void Deletea_Country(int countryID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Countries_del");
                
                database.AddInParameter(dbCommand, "@CountryID", DbType.Int32, countryID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Country methods
        
        public virtual a_Country Geta_Country(int countryID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Countries_sel");
                
                database.AddInParameter(dbCommand, "@CountryID", DbType.Int32, countryID);
                
                a_Country a_Country =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Country = Createa_CountryFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Country;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_CountryList methods
        public virtual a_CountryCollection Geta_CountryList(a_CountryColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Countries_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_CountryCollection a_CountryCollection = new a_CountryCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Country a_Country = Createa_CountryFromReader(reader);
                        a_CountryCollection.Add(a_Country);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_CountryCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_CountryCollection Geta_CountryList(a_CountryColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_CountryList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion

        #region Geta_CountryList_alphabet methods
        public virtual a_CountryCollection Geta_CountryList_alphabet(string mychar, a_CountryColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Countries_selList_alphabet");

                database.AddInParameter(dbCommand, "@Mychar", DbType.AnsiString, mychar);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_CountryCollection a_CountryCollection = new a_CountryCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Country a_Country = Createa_CountryFromReader(reader);
                        a_CountryCollection.Add(a_Country);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_CountryCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_CountryCollection Geta_CountryList_alphabet(string mychar, a_CountryColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_CountryList_alphabet(mychar, orderBy, orderDirection, 0, 0, out totalRecords);
        }

        #endregion
    }
}